package com.my.blog.myblog.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.blog.annotation.SystemLog;
import com.my.blog.myblog.entity.Blog;
import com.my.blog.myblog.entity.Blogtype;
import com.my.blog.myblog.service.BlogTypeService;
import com.my.blog.utils.ConstantUtil;
import com.my.blog.utils.ResultBean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/***
 *
 * @Auther: MuYun
 * @Date: 2021/9/16 14:27
 * @Description： 后台博客类别的controller
 *
 */
@RestController
@RequestMapping("/admin")
public class AdminBlogTypeConroller {

    @Resource
    private BlogTypeService blogTypeService;

    @PostMapping("/selectBlogTypeListByPage")
    public ResultBean<IPage<Blogtype>> selectBlogTypeListByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){

        Page<Blogtype> blogtypePage = blogTypeService.page(new Page<>(page, pageSize));
        ResultBean<IPage<Blogtype>> resultBean=new ResultBean<>();
        if(ObjectUtil.isNotEmpty(blogtypePage.getRecords())){
            resultBean.setData(blogtypePage);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    @PostMapping("/selectBlogTypeById")
    public ResultBean<Blogtype> selectBlogTypeById(@RequestParam(value = "id")String id){
        ResultBean<Blogtype> resultBean=new ResultBean<>();
        Blogtype blogtype = blogTypeService.getById(id);
        if(ObjectUtil.isNotEmpty(blogtype)){
            resultBean.setData(blogtype);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    @PostMapping("/addBlogType")
    @SystemLog(description = ConstantUtil.BLOGTYPE_ADD,userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> addBlogType(Blogtype blogtype){
        ResultBean<Object> resultBean=new ResultBean<>();
        blogtype.setAddTime(LocalDateTime.now());
        if(blogTypeService.save(blogtype)){
            resultBean.setCode(ResultBean.SUCCESS);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    @PostMapping("/updateBlogType")
    @SystemLog(description = ConstantUtil.BLOGTYPE_UPDATE,userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> updateBlogType(Blogtype blogtype){
        ResultBean<Object> resultBean=new ResultBean<>();
        if(blogTypeService.updateById(blogtype)){
            resultBean.setCode(ResultBean.SUCCESS);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    @PostMapping("/deleteBlogType")
    @SystemLog(description = ConstantUtil.BLOGTYPE_DELETE,userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> deleteBlogType(@RequestParam(value = "id")String id){
        ResultBean<Object> resultBean=new ResultBean<>();
        if(blogTypeService.removeById(id)){
            resultBean.setCode(ResultBean.SUCCESS);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;

    }
}
